<template>
  <div class="userManagement-container">
    <!-- <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          新增
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form> -->

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <!-- <el-table-column show-overflow-tooltip type="selection"></el-table-column> -->
      <el-table-column
        show-overflow-tooltip
        prop="account"
        :label="$t('order.nameOfSession') /*场次名称*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="account"
        :label="$t('order.deadline') /*截止时间*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        :label="$t('common.operate') /*操作*/"
        width="300"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row, 'edit')">
            {{ $t('accountManage.edit') /*编辑*/ }}
          </el-button>

          <el-button type="text" @click="handleDelete(row)">
            {{ $t('accountManage.delete') /*删除*/ }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination
      background
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination> -->
    <edit ref="edit" @fetch-data="fetchData"></edit>
    <!-- <changci-management-edit
      ref="changci"
      @fetch-data="fetchData"
    ></changci-management-edit> -->
  </div>
</template>

<script>
  import { doDelete, getList } from '@/api/userManagement'
  // import ChangciManagementEdit from './components/changciManagementEdit'
  import userData from '@/views/accountManagement/userManagement/data.json'
  import Edit from './components/UserManagementEdit'
  export default {
    name: 'UserManagement',
    components: { Edit },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          username: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row, type = 'edit') {
        if (row.id) {
          this.$refs[type].showEdit(row)
        } else {
          this.$refs[type].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm(
            `${this.$t(
              'accountManage.areYouSureDeleteCurrent'
            )}` /*你确定要删除当前项吗*/,
            null,
            async () => {
              const { msg } = await doDelete({ ids: row.id })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            }
          )
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm(
              `${this.$t(
                'accountManage.areYouSureDeleteSel'
              )}` /*你确定要删除选中项吗*/,
              null,
              async () => {
                const { msg } = await doDelete({ ids })
                this.$baseMessage(msg, 'success')
                this.fetchData()
              }
            )
          } else {
            this.$baseMessage(
              `${this.$t('accountManage.notSelAnyRow')}` /*未选中任何行*/,
              'error'
            )
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data, totalCount } = userData || (await getList(this.queryForm))
        console.log(data, totalCount, '获取数据')
        this.list = data
        this.total = totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
